package ch.fhnw.qualiscan.application.controllers;

import java.util.Vector;

import com.vaadin.data.Validator.InvalidValueException;
import com.vaadin.data.util.BeanItem;
import com.vaadin.ui.Button;
import com.vaadin.ui.Form;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;

import ch.fhnw.qualiscan.Config;
import ch.fhnw.qualiscan.application.models.SurveyModel;
import ch.fhnw.qualiscan.application.models.UserModel;
import ch.fhnw.qualiscan.application.views.NewAccountView;
import ch.fhnw.qualiscan.core.Controller;
import ch.fhnw.qualiscan.core.db.fieldfactories.NewUserFieldFactory;
import ch.fhnw.qualiscan.core.db.pojos.SurveyUserRole;
import ch.fhnw.qualiscan.core.db.pojos.User;
import ch.fhnw.qualiscan.core.helpers.EmailHelper;
import ch.fhnw.qualiscan.core.helpers.UserSession;
import ch.fhnw.qualiscan.core.libraries.NotificationLibrary;
import eu.livotov.tpt.i18n.TM;

public class NewAccount extends Controller {
	
	private UserModel userModel;
	private SurveyModel surveyModel;
	private SurveyUserRole invitationSurvey;
	private String hash;
	
	
	@Override
	public void init() {
		
	
	}
	
	public boolean saveUser(User user) {
		return userModel.saveUser(user);
		
	}
	
	public void updateUser(User user) {
		EmailHelper email = new EmailHelper();
		email.sendConfirmationMail(user);
		userModel.updateUser(user);
	}

	@Override
	public void setView() {
		this.view = new NewAccountView(this);
		view.buildLayout();
	}

	@Override
	public void loadModels() {
		userModel = new UserModel();
		surveyModel = new SurveyModel();
		
	}
	public String getHash() {
		return hash;
	}
	
	public SurveyUserRole getInvitationSurvey() {
		return invitationSurvey;
	}
	
	private void getSur() {
		
		invitationSurvey = surveyModel.getSurveyUserRoleByHash(hash);
	}
	
	public User getUser() {
		return userModel.getUserById(invitationSurvey.getUser().getId());
		
	}
	
	@Override
	public void setNeededPermissions() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void urlFragmentChanged(int fragmentId, String fragment) {
		if (fragmentId == 2) {
			if (!fragment.equals("")) {
				hash = fragment;
				getSur();
				view.buildLayout();
			}
		}
		
		
		
	}

	

	

}
